//
//  RadioController.m
//  ProductB
//
//  Created by 田阳 on 16/8/5.
//  Copyright © 2016年 田阳. All rights reserved.
//

#import "RadioController.h"
#import "TypeModel.h"
#import "ShangHaiModel.h"
#import "RankModel.h"
#import "RadioTableViewCell.h"
#import "RadioListViewController.h"
@interface RadioController ()<UITableViewDataSource,UITableViewDelegate>

//全页面的tableView
@property(nonatomic,strong) UITableView *MainTableView;

// 最上方本地台、国家台,新闻台等数组。
@property(nonatomic,strong) NSMutableArray *typeArray;

//早按上海数组
@property(nonatomic,strong) NSMutableArray *ShangHaiArray;

//排行榜数组
@property(nonatomic,strong) NSMutableArray *rankArray;

//保存上面几个数组的数组
@property(nonatomic,strong) NSMutableArray *allArray;

//手势
@property (nonatomic, strong)UITapGestureRecognizer *tap;
// table 头视图
@property (nonatomic, strong)UIView *headView;
// 记录头视图的高度
@property (nonatomic, assign) CGFloat headViewheight;
// 头视图上的5个视图
@property (nonatomic, strong)UIView *loaclV;
@property (nonatomic, strong)UIView *nationV;
@property (nonatomic, strong)UIView *provinceV;
@property (nonatomic, strong)UIView *networkV;
@property (nonatomic, strong)UIView *buttonView;
// 分区头视图
@property (nonatomic, strong)UIView *sectionV1;
@property (nonatomic, strong)UIView *sectionV2;
// 中间的新闻台等button
@property (nonatomic, strong)UIButton *typeBtn;




@end

@implementation RadioController

-(NSMutableArray *)typeArray {
    if (!_typeArray) {
        _typeArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _typeArray;
}


-(NSMutableArray *)ShangHaiArray {
    if (!_ShangHaiArray) {
        _ShangHaiArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _ShangHaiArray;
}


-(NSMutableArray *)rankArray {
    if (!_rankArray) {
        _rankArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _rankArray;
}


-(NSMutableArray *)allArray {
    if (!_allArray) {
        _allArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _allArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //添加背景模糊视图
    self.MainTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.MainTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mohu.png"]];
    //设置tableView头视图
    [self.view addSubview:self.MainTableView];
    [self getData];
}

-(void)getData {
    [NetWorkrequestManage requestWithType:GET url:KRadioURL parameters:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.typeArray = [TypeModel ModelConfigureWithJsonDic:dic];
        self.ShangHaiArray = [ShangHaiModel ModelConfigureWithJsonDic:dic];
        self.rankArray = [RankModel ModelConfigureWithJsonDic:dic];
        self.allArray = [NSMutableArray arrayWithObjects:self.ShangHaiArray,self.rankArray, nil];
        dispatch_async(dispatch_get_main_queue(), ^{
        [self creatTableView];
        [self.MainTableView reloadData];

        });
    } error:^(NSError *error) {
        NSLog(@"解析出错");
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}


-(void)creatTableView {
    self.MainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - self.tabBarController.tabBar.ownHeight) style:UITableViewStyleGrouped];
    self.MainTableView.delegate = self;
    self.MainTableView.dataSource = self;
    [self.view addSubview:self.MainTableView];
    [self.MainTableView registerNib:[UINib nibWithNibName:@"RadioTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [self creatHeaderView];
    self.MainTableView.tableHeaderView = self.headView;
}


-(void)creatHeaderView {
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    self.headView.backgroundColor = [UIColor whiteColor];
    [self creatTopButtonView];
    [self creatTypeButtonView];
    
}

//最上三个按钮
-(void)creatTopButtonView {
    // 省市台,省_1.png
    NSArray *imageArr = @[@"local.png",@"country.png",@"internet.png"];
    NSArray *textArr = @[@"本地台",@"国家台",@"网络台"];
    for (NSInteger i = 0; i < 3; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(i*kScreenWidth/3, 0, kScreenWidth/3, 70)];
        [self creatTopView:view buttonImage:imageArr[i] labelText:textArr[i] buttonTag:i+100];
        [self.headView addSubview:view];
    }
}


-(void)creatTopView:(UIView *)view buttonImage:(NSString *)imageName labelText:(NSString *)text buttonTag:(NSInteger)tag {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(view.frame.size.width/2-15, 10, (view.frame.size.width - 20)/3, 30);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.tintColor = [UIColor greenColor];
    button.tag = tag;
    [button addTarget:self action:@selector(topaction:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width/2-30, 50, 60, 20)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = text;
    label.font = [UIFont systemFontOfSize:20];
    [view addSubview:label];
    [view addSubview:button];
}

#pragma mark --- 点击最上面四个按钮 页面跳转
- (void)topaction:(UIButton *)button {
    RadioListViewController *radiolistVC = [[RadioListViewController alloc]init];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:radiolistVC animated:YES];
}

#pragma mark ----- 创建新闻台、音乐台等电台按钮所在视图 -----
- (void)creatTypeButtonView {
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(10,80, kScreenWidth - 20, 60)];
    self.buttonView.backgroundColor = [UIColor whiteColor];
    [self.headView addSubview:self.buttonView];
    // 创建电台按钮
    [self creatTypeButton];
}

#pragma mark ----- 创建电台按钮 -----

-(void)creatTypeButton{
    NSInteger count = 0;
    for (TypeModel *model in self.typeArray) {
        self.typeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        self.typeBtn.frame = CGRectMake(count%4 * (self.buttonView.ownWidth/4)-4,count/4 * 30,self.buttonView.ownWidth/4-4, 26);
        self.typeBtn.backgroundColor = [UIColor whiteColor];
        if (count == 7) {
            [self.typeBtn setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
            self.typeBtn.tintColor = [UIColor orangeColor];
            [self.typeBtn addTarget:self action:@selector(nextView2:) forControlEvents:UIControlEventTouchUpInside];
            self.typeBtn.tag = 999;
            [self.buttonView addSubview:self.typeBtn];
            return;
        }
        self.typeBtn.tag = [model.idd integerValue];
        [self.typeBtn setTitle:model.name forState:UIControlStateNormal];
        [self.typeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.typeBtn.backgroundColor = [UIColor whiteColor];
        [self.typeBtn addTarget:self action:@selector(nextView1:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonView addSubview:self.typeBtn];
        count++;
    }
}

#pragma mark ----- 电台视图 扩展 -----
-(void)nextView2:(UIButton *)button{
    button = [self.view viewWithTag:999];
    CGRect newFrame = self.headView.frame;
    newFrame.size.height = newFrame.size.height + self.buttonView.frame.size.height;
    self.headView.frame = newFrame;
    self.buttonView.ownHeight = self.headView.frame.size.height-80;
    
    [self.MainTableView beginUpdates];
    [self.MainTableView setTableHeaderView:self.headView];
    [self.MainTableView endUpdates];
    [self.MainTableView reloadData];
    
    NSInteger count = 0;
    for (TypeModel *model in self.typeArray) {
        if (count >= 7) {
            [button removeFromSuperview];
            self.typeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            self.typeBtn.frame = CGRectMake(count%4 * (self.buttonView.ownWidth/4)-4,count/4 * 30,self.buttonView.ownWidth/4 -4, 26);
            self.typeBtn.backgroundColor = [UIColor whiteColor];
            self.typeBtn.tag = [model.idd integerValue];
            [self.typeBtn setTitle:model.name forState:UIControlStateNormal];
            [self.typeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.typeBtn addTarget:self action:@selector(nextView1:) forControlEvents:UIControlEventTouchUpInside];
            [self.buttonView addSubview:self.typeBtn];
        }
        count++;
    }
    self.typeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.typeBtn.frame = CGRectMake(3 * (self.buttonView.ownWidth/4)-4,3 * 30,self.buttonView.ownWidth/4 -4, 26);
    self.typeBtn.backgroundColor = PKCOLOR(241, 241, 241);
    self.typeBtn.tag = 998;
    [self.typeBtn setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
    self.typeBtn.tintColor = [UIColor orangeColor];
    self.typeBtn.backgroundColor = [UIColor whiteColor];
    [self.typeBtn addTarget:self action:@selector(nextView3:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonView addSubview:self.typeBtn];
    
}

#pragma mark ----- 电台视图 收缩 -----
-(void)nextView3:(UIButton *)button{
    
    CGRect newFrame = self.headView.frame;
    newFrame.size.height = 140;
    self.headView.frame = newFrame;
    self.buttonView.ownHeight = self.headView.ownHeight/2 -10;
    
    [self.MainTableView beginUpdates];
    [self.MainTableView setTableHeaderView:self.headView];
    [self.MainTableView endUpdates];
    [self.MainTableView reloadData];
    for (TypeModel *model in self.typeArray) {
        button = [self.view viewWithTag:[model.idd integerValue]];
        [button removeFromSuperview];
    }
    button = [self.view viewWithTag:998];
    [button removeFromSuperview];
    [self creatTypeButton];
}

#pragma mark ----- 新闻台、音乐台等 跳转方法 -----
-(void)nextView1:(UIButton *)button{
    //    NSLog(@"%ld", button.tag);
    RadioListViewController *radioLsitVC = [[RadioListViewController alloc]init];
    radioLsitVC.categoryId = button.tag;
    radioLsitVC.topTitleName = button.titleLabel.text;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:radioLsitVC animated:YES];
}



#pragma mark ----- 新闻台、音乐台等 跳转方法 -----
-(void)nextView:(UIButton *)button{
    //    NSLog(@"%ld", button.tag);
    RadioListViewController *radioListVC = [[RadioListViewController alloc]init];
    radioListVC.categoryId = button.tag;
    radioListVC.topTitleName = button.titleLabel.text;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:radioListVC animated:YES];
}


#pragma mark ----- 创建tableViewSection头视图 -----
-(UIView *)sectionV1{
    if (!_sectionV1) {
        _sectionV1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.ownHeight/12)];
        _sectionV1.backgroundColor = [UIColor whiteColor];
        [self creatSectionHeaderViewWithView:_sectionV1 image:@"播放.png" labelText:@"早安·上海" label1Text:@"更多 >" buttonTag:1];
    }
    return _sectionV1;
}



-(UIView *)sectionV2{
    if (!_sectionV2) {
        _sectionV2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.ownHeight/12)];
        _sectionV2.backgroundColor = [UIColor whiteColor];
        [self creatSectionHeaderViewWithView:_sectionV2 image:@"播放.png" labelText:@"排行榜" label1Text:@"更多 >" buttonTag:2];
    }
    return _sectionV2;
}


- (void)creatSectionHeaderViewWithView:(UIView *)view image:(NSString *)imageName labelText:(NSString *)text label1Text:(NSString *)text1 buttonTag:(NSInteger)tag{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = view.frame;
    [button addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    button.tag = tag;
    [view addSubview:button];
    
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 10, 10)];
    imageV.image = [UIImage imageNamed:imageName];
    [button addSubview:imageV];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(24, 10, 100, 22)];
    label.text = text;
    [button addSubview:label];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 55, 10, 55, 22)];
    label1.text = text1;
    label1.textColor = [UIColor lightGrayColor];
    [button addSubview:label1];
}


#pragma mark --- 更多 方法
- (void)btnAction:(UIButton *)button {
    RadioListViewController *RadioListVC = [[RadioListViewController alloc]init];
    // 传值过去，用于判断使用什么 URL.
    if (button.tag == 1) {
        RadioListVC.locationId = 1;
    }else {
        RadioListVC.rankId = 1;
    }
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:RadioListVC animated:YES];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.allArray.count;
}


//tableView数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.allArray[section] count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.ownHeight/5;
}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return self.sectionV1;
    } else {
        return self.sectionV2;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return self.view.ownHeight/15;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        ShangHaiModel *model = self.ShangHaiArray[indexPath.row];
        cell.playButton.tag = indexPath.row + 100;
        [cell cellConfigureWithShangHaiModel:model];
        [cell.playButton addTarget:self action:@selector(playAction1:) forControlEvents:(UIControlEventTouchUpInside)];
    } else{
        RankModel *model = self.rankArray[indexPath.row];
        cell.playButton.tag = indexPath.row + 200;
        [cell cellConfigureWithRankModel:model];
        [cell.playButton addTarget:self action:@selector(playAction1:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return cell;
}

- (void)playAction1:(UIButton *)btn{
    NSLog(@"222");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
